<template>
  <!-- 添加或修改采购单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="采购日期:" prop="ddDate">
            <el-date-picker clearable
                            v-model="form.ddDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择采购日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请金额:" prop="money">
            <el-input-number v-model="form.money" disabled></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购类型" prop="ywType">
            <el-select v-model="form.ywType" placeholder="请选择采购类型" clearable disabled>
              <el-option v-for="dict in dict.type.caigou_type"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商:" prop="supplierId">
            <el-select v-model="form.supplierId" placeholder="请选择供应商" filterable @change="selectSupplier"
                       style="width: 260px">
              <el-option v-for="dict in dict.type.goods_supplier" ref="myselectSsupplier"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="开户行:" prop="bankName">
            <el-input v-model="form.bankName" placeholder="请输入开户行"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行行号:" prop="bankNo">
            <el-input v-model="form.bankNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账号:" prop="accountNo">
            <el-input v-model="form.accountNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注:">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";

export default {
  name: "LvBuyIronForm",
  dicts: ['goods_customer', 'company', 'yes_no', 'goods_supplier', 'goods_type', 'caigou_type', 'yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: null,
        supplierId: null,
        supplierName: null,
        dingjin: null,
        shippingFee: null,
        ddDate: null,
        bankName: null,
        bankNo: null,
        accountNo: null,
        ironRecordId: null,
        money: null,
        remark: null,
        goodsType: "0",
      },
      // 表单校验
      rules: {
        supplierId: [
          {required: true, message: "请选择供应商", trigger: "blur"}
        ],
        bankName: [
          {required: true, message: "请输入开户行", trigger: "blur"}
        ],
        accountNo: [
          {required: true, message: "请输入银行账号", trigger: "blur"}
        ],
        ywType: [
          {required: true, message: "请选择采购类型", trigger: "blur"}
        ],
      },
      url: {
        getById: '/lvBuy/lvBuy/',
        add: '/lvBuy/lvBuy/addBuyIron',
        edit: '/lvBuy/lvBuy/edit',
        goodsSupplierById: '/goodsSupplier/goodsSupplier/',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        money: null,
        dingjin: null,
        shengyu: null,
        shuie: null,
        counts: null,
        dunwei: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        state: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAddBuyIronRecord(row) {
      this.reset();
      this.open = true;
      this.form.ironRecordId = row.id;
      this.form.money = row.money;
      this.form.ywType = "9";
      this.form.ddDate = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
      this.title = "添加铁件采购单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改铁件采购单";
      });
    },

    selectSupplier(value) {
      let that = this;
      that.$nextTick(() => {
        let arr = that.$refs.myselectSsupplier;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].currentValue == value) {
            that.form.supplierName = arr[i].label;
            getById(this.url.goodsSupplierById, value).then(response => {
              that.$set(that.form, 'bankName', response.data.bankName);
              that.$set(that.form, 'accountNo', response.data.accountNo);
              that.$set(that.form, 'bankNo', response.data.bankNo);
            });
          }
        }
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },
  }
};
</script>
